module repository.FriendRepository;

import hunt.framework;
import model;
import core.stdc.time;
import hunt.logging;
import std.conv : to;
import bootstrap.DatabaseBootstrap;

class FriendRepository : EntityRepository!( Friend , int)
{
  this()
  {
    super(defaultEntityManager());
  }

	Friend[] findAllByID(int uid)
	{
    scope(exit)
    {
      _manager.close();
    }
		return findAll(new Condition("%s = %s" , Field.userId , uid));
	}

  Friend[] findAllBychatId(int chatId)
  {
    scope(exit)
    {
      _manager.close();
    }
    return findAll(new Condition("%s = %s" , Field.chatId , chatId));
  }

	Friend findByChatId(int chatId , int uid)
	{
		return find(new Condition("%s = %s and %s = %s" , Field.chatId , chatId , Field.userId , uid));
	}

	Friend findByUid(int uid , int friendid)
	{
		return find(new Condition("%s = %s and %s = %s" ,Field.userId , uid , Field.friendId , friendid));
	}


  void putSelf(string userId, string chatId)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto insert = _manager.createQuery!(Friend)("INSERT INTO Friend u(u.chatId,u.userId,u.friendId,u.created,u.src) values (:chatId,:userId,:friendId,:time,:src)");
    insert.setParameter("chatId",chatId);
    insert.setParameter("userId",userId);
    insert.setParameter("friendId",userId);
    insert.setParameter("time",core.stdc.time.time(null));
    insert.setParameter("src",0);
    insert.exec();
  }

  void putSingle(string userId, string friendId ,string chatId, int src)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto insert = _manager.createQuery!(Friend)("INSERT INTO Friend u(u.chatId,u.userId,u.friendId,u.created,u.src) values (:chatId,:userId,:friendId,:time,:src)");
    insert.setParameter("chatId",chatId);
    insert.setParameter("userId",userId);
    insert.setParameter("friendId",friendId);
    insert.setParameter("time",core.stdc.time.time(null));
    insert.setParameter("src",src);
    insert.exec();
  }

  void putFriend(string userId, string friendId ,string chatId, int src)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto insert = _manager.createQuery!(Friend)("INSERT INTO Friend u(u.chatId,u.userId,u.friendId,u.created,u.src) values (:chatId,:userId,:friendId,:time,:src)");
    insert.setParameter("chatId",chatId);
    insert.setParameter("userId",userId);
    insert.setParameter("friendId",friendId);
    insert.setParameter("time",core.stdc.time.time(null));
    insert.setParameter("src",src);
    insert.exec();

    insert = _manager.createQuery!(Friend)("INSERT INTO Friend u(u.chatId,u.userId,u.friendId,u.created,u.src) values (:chatId,:friendId,:userId,:time,:src)");
    insert.setParameter("chatId",chatId);
    insert.setParameter("friendId",friendId);
    insert.setParameter("userId",userId);
    insert.setParameter("time",core.stdc.time.time(null));
    insert.setParameter("src",src);
    insert.exec();
  }

  bool setAliasName(string userId, string chatId, string aliasName)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto update = _manager.createQuery!(Friend)("UPDATE Friend u SET u.aliasName = :aliasName WHERE u.chatId = :chatId and u.userId = :userId");
    update.setParameter("aliasName",aliasName);
    update.setParameter("chatId",to!uint(chatId));
    update.setParameter("userId",to!uint(userId));
    try{
      update.exec();
    }
    catch(Exception e)
    {
      logError("setAliasName error : %s",e.msg);
      return false;
    }
    return true;
  }

  void updateMsgRead(string userId , string chatId, long timestamp)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto update = _manager.createQuery!(Friend)("UPDATE Friend u SET u.updated = :timestamp WHERE u.chatId = :chatId and u.userId = :userId");
    update.setParameter("timestamp",timestamp);
    update.setParameter("chatId",to!uint(chatId));
    update.setParameter("userId",to!uint(userId));
    try{
      update.exec();
    }
    catch(Exception e)
    {
      logError("updateMsgRead error : %s",e.msg);
    }
  }

  void deleteFriend(string userId,string friendId)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto update = _manager.createQuery!(Friend)("DELETE FROM Friend u WHERE u.userId = :userId AND u.friendId = :friendId");
    update.setParameter("userId",userId);
    update.setParameter("friendId",friendId);
    try{
      update.exec();
    }
    catch(Exception e)
    {
      logError("deleteFriend error : %s",e.msg);
    }

    //update = _manager.createQuery!(Friend)("DELETE FROM Friend u WHERE u.userId = :userId AND u.friendId = :friendId");
    //update.setParameter("userId",friendId);
    //update.setParameter("friendId",userId);
    //try{
    //  update.exec();
    //}
    //catch(Exception e)
    //{
    //  logError("deleteFriend error : %s",e.msg);
    //}
  }
}
